﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReviewCurveApp.Classes;
using ReviewCurveApp.Contorller;

namespace ReviewCurveApp
{
    public partial class FrmTasksView : Form
    {
        public FrmTasksView()
        {
            InitializeComponent();
        }

        UnitsManager unitsManager = new UnitsManager();
        
        TasksManager tasksManager = new TasksManager();
        
        ReviewsManager reviewsManager = new ReviewsManager();
        
        List<Task> _tasks = new List<Task>();
        


        private void LoadTasks(DateTime dateTime)
        {
            ClearUI();
            
            _tasks = new List<Task>();

            List<Task> tasks = unitsManager.getTasksByDay(dateTime);
            
            //reviewsManager.LoadReviewRecords(dateTime);
            
            var query = from task in tasks
                        orderby task.ScheduledTime ascending /*descending*/
                        select task;
            int i=0;

            foreach (var item in query)
            {
                Task task = item as Task;
                ListViewItem viewItem = new ListViewItem();

                viewItem.Text = (++i).ToString() ;
                viewItem.SubItems.Add( task.ToString());
                viewItem.Tag = task;
                viewItem.Group = SetGroup(task, dateTime); //SetGroup(task.Sequnce);
                //bool hasDone = reviewsManager.HasDone(task);

                if (task.Status==TaskStatus.Finished)
                {
                    viewItem.Checked = true;
                    viewItem.ForeColor = Color.Gray;
                    viewItem.BackColor = Color.Silver;
                } 

                listView_tasks.Items.Add(viewItem);

                //checkedListBox_Tasks.Items.Add((++i).ToString() + " " + task.ToString(), false);
                _tasks.Add(task);
            }
            listView_tasks.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);

        }

        private ListViewGroup SetGroup(int sequence)
        {
            if (sequence==1)
            {
                return listView_tasks.Groups["NewUnitTasks"];
            }
            return listView_tasks.Groups["ReviewTasks"];
        }

        private ListViewGroup SetGroup(Task task ,DateTime dateTime)
        {
            if (task.Sequnce == 1)
            {
                return listView_tasks.Groups["NewUnitTasks"];
            }
            if (task.ScheduledTime<dateTime)
            {
                return listView_tasks.Groups["DelayedTasks"];
            }

            return listView_tasks.Groups["ReviewTasks"];
        }

        private void ClearUI()
        {
           
            listView_tasks.Items.Clear();
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            LoadTasks(e.Start);
        }

 

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            LoadTasks(dateTimePicker1.Value);
        }

        private void button_goforward_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Value = dateTimePicker1.Value.AddDays(-1);
        }

        private void button_gobackward_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Value = dateTimePicker1.Value.AddDays(1);
        }

        private void FrmTasksView_Load(object sender, EventArgs e)
        {
            LoadTasks(dateTimePicker1.Value);
        }

         private void toolStripButtonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

 

        private void DeleteRecord()
        {
            Task task = GetSelectedTask();
            label1.Text = "删除记录\t" + task.ToString();
            
            tasksManager.DeleteRecord(task);
            
        }


        private void RecordCompleted()
        {
            Task task = GetSelectedTask();
            //task.CompletedDateTime = dateTimePicker1.Value;
            task.CompletedDateTime = DateTime.Today;
            bool HasDone = tasksManager.SetComplete(task);
            //task.Id = i;
            if (HasDone)
            {
                label1.Text = "记录成功！";
                LoadTasks(dateTimePicker1.Value);
                return;
            } 
                 label1.Text = "记录失败！";           
        }

        private void RecordCompleted1()
        {
            Task task = GetSelectedTask();
            
            int i=tasksManager.AddRecord(task);
            task.Id = i;
            label1.Text = "添加记录\t"+i.ToString()+"\t" + task.ToString();
        }

        private void listView_tasks_DoubleClick(object sender, EventArgs e)
        {

            if (listView_tasks.SelectedItems[0].Checked)
            {
                RecordCompleted();
            }
            else
            {
                DeleteRecord();
            }
        }

        private Task GetSelectedTask()
        {
            return listView_tasks.SelectedItems[0].Tag as Task;
        }

        private void toolStripButton_BrowseSubjects_Click(object sender, EventArgs e)
        {
            FrmSubjectsView frmSubjectsView = new FrmSubjectsView();
            frmSubjectsView.ShowDialog();
            LoadTasks(dateTimePicker1.Value);
        }



    }
}
